package cn.wolfcode.trip.app.controller;

import cn.wolfcode.trip.app.util.FreemarkUtil;
import cn.wolfcode.trip.base.domain.ScenicSpot;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.service.IScenicSpotService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

//页面的景点的接口
@RestController
@RequestMapping("/scenicSpot")
public class scenicSpotController {
@Autowired
private IScenicSpotService scenicSpotService;
    @Autowired
    private ServletContext ctx;

    //通过地区id去查询景点
    @GetMapping(value = "/{regionId}/region",produces = "text/html;charset=UTF-8")
    public void getScenicSpotByRegionId(@PathVariable Long regionId, QueryObject qo, HttpServletResponse resp) throws Exception {
        String dir = ctx.getRealPath("/template");
        Map<String, Object> map = new HashMap<>();
        map.put("regionId",regionId);
        qo.setPageSize(2);
        PageInfo<ScenicSpot> pageInfo=scenicSpotService.getScenicSpotByRegionId(regionId, qo);
        map.put("info",pageInfo);
        FreemarkUtil.writeData(dir, "scenicSpotContent.ftl", map, resp.getWriter());
    }

    //查询所有的景点
    @GetMapping
    public Object getAllScenicSpot(QueryObject qo) {
        return scenicSpotService.query(qo);
    }

    //查询指点的景点id
    @GetMapping("/{scenicSpotId}")
    public Object getScenicSpot(@PathVariable Long scenicSpotId){
        return scenicSpotService.getscenicSpotById(scenicSpotId);
    }


}
